package com.online.onlineshop.controllers;

import com.online.onlineshop.entity.Orders;
import com.online.onlineshop.services.ForOrderService;
import com.online.onlineshop.utils.ResultForFrontEnd;
import com.online.onlineshop.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("order")
@RestController
public class OrderController extends RootController {
    @Autowired
    private ForOrderService forOrderService;

    /*
     * 生成订单的控制层方法
     *
     * */
    @RequestMapping("product")
    public ResultForFrontEnd<Orders> product(Integer aid, Integer[] cids, HttpSession session) {
        int uid = getUidFromSession(session);
        String userName = getUserNameFromSession(session);
        Orders data = forOrderService.product(aid, uid, userName, cids);
        return new ResultForFrontEnd<>(ok, data);
    }

    @RequestMapping({"", "/"})
    private ResultForFrontEnd<List<OrderVO>> selectOrderVO(HttpSession session) {
        int uid = getUidFromSession(session);
        List<OrderVO> data = forOrderService.selectOrderVO(uid);
        return new ResultForFrontEnd<>(ok, data);
    }

    @RequestMapping("selectById")
    private ResultForFrontEnd<Orders> selectById(int oid) {
        Orders data = forOrderService.selectById(oid);
        return new ResultForFrontEnd<>(ok, data);
    }

}
